package com.xianbeiying.app.ckgl.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.LoginBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.SetModel;
import com.xianbeiying.app.ckgl.util.SharedPreferencesUtils;
import com.xianbeiying.app.ckgl.util.ToastUtil;

/**
 * 修改密码
 */
public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener, HttpCallBackUtil {
    private ImageView iv_title_left;
    private TextView tv_title;
    private LinearLayout ly_initial_pwd;
    private View view_line1,view_line2;
    private EditText et_initial_pwd, et_rest_pwd, et_arest_pwd,et_arest_pwd2;
    private Button rest_btn;
    private SetModel setModel;
    private View line1,line2;
    private LinearLayout ly_rest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        initView();
    }
    private void initView() {
        setModel=new SetModel(this);
        setModel.addResponseListener(this);
        ly_rest = (LinearLayout) findViewById(R.id.ly_rest);
        line1 = (View) findViewById(R.id.line1);
        line2 = (View) findViewById(R.id.line2);
        line1.setVisibility(View.VISIBLE);
        line2.setVisibility(View.VISIBLE);
        ly_rest.setVisibility(View.VISIBLE);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        ly_initial_pwd = (LinearLayout) findViewById(R.id.ly_initial_pwd);
        view_line1 = (View) findViewById(R.id.view_line1);
        view_line2 = (View) findViewById(R.id.view_line2);
        et_initial_pwd = (EditText) findViewById(R.id.et_initial_pwd);
        et_arest_pwd2=(EditText) findViewById(R.id.et_arest_pwd2);
        et_rest_pwd = (EditText) findViewById(R.id.et_rest_pwd);
        et_arest_pwd = (EditText) findViewById(R.id.et_arest_pwd);
        rest_btn = (Button) findViewById(R.id.rest_btn);
        tv_title.setText("修改密码");
        iv_title_left.setOnClickListener(this);
        rest_btn.setOnClickListener(this);
    }

    @Override
    public void onCallBackData(String url, Object content) {
        if(url.equals("修改密码")){
            LoginBean lg = JSON.parseObject(new String((byte[]) content), LoginBean.class);
            if (lg.isR()){
                ToastUtil.showInfo(ChangePasswordActivity.this, "修改成功");
                finish();
            } else{
                ToastUtil.showInfo(ChangePasswordActivity.this, lg.getERR_MSG());
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.rest_btn:
                String restpwd2 =et_arest_pwd2.getText().toString().trim();
                String restpwd = et_rest_pwd.getText().toString().trim();
                String arestpwd = et_arest_pwd.getText().toString().trim();
//                if(initialpwd.isEmpty()){
//                    ToastUtil.showInfo(this,"用户名不能为空");
//                    return;
//                }
                if(restpwd.isEmpty()){
                    ToastUtil.showInfo(this,"原始密码不能为空");
                    return;
                }
                if(arestpwd.isEmpty()){
                    ToastUtil.showInfo(this,"新密码不能为空");
                    return;
                }
                if(restpwd2.isEmpty()){
                    ToastUtil.showInfo(this,"确认密码不能为空");
                    return;
                }
                if(!arestpwd.equals(restpwd2)){
                    ToastUtil.showInfo(this,"两次输入密码不一致");
                    return;
                }
                setModel.ChangePaw(SharedPreferencesUtils.getStringDate("username"),arestpwd,restpwd,true);
                break;
        }
    }

}
